<template>
  <div class="container">
    <div class="btnList">
      <el-date-picker
        v-model="erpCenter.date"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="success"
        class="handleSearch"
        icon="el-icon-check"
        @click="pull()"
        >拉取ERP</el-button
      >
    </div>
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.searchCode"
        placeholder="请输入分货单编码"
        clearable
      ></el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend"
          >商品</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <!-- <el-select
        v-model="search.erpCode"
        :multiple="false"
        multiple
        collapse-tags
        placeholder="请选择商品编码"
        :remote-method="remoteMethod"
        :loading="outer.loading"
      >
        <el-option
          v-for="item in outer.options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select> -->
      <!-- <el-date-picker
        v-model="search.date"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
      ></el-date-picker>-->

      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索仓库"
        v-model="warehouseList.name"
      >
        <template slot="prepend"
          >仓库</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="warehouseBtn"
        ></el-button>
      </el-input>

      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-select v-model="search.status" placeholder="请选择状态">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select
        v-model="search.hangcai"
        style="margin-left: 20px"
        placeholder="请选择是否行采"
      >
        <el-option
          v-for="item in hangcaiList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derives()"
        >导出明细</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="pickingcode"
        label="分货单编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="productcode"
        label="商品编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="productname"
        label="商品名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="add_time"
        label="拉取时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="deliver_time"
        label="发货时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusMsg"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="hangcai_desc"
        label="行采状态"
        align="center"
      ></el-table-column>
      <el-table-column prop="des" label="备注" align="center"></el-table-column>
      <el-table-column
        prop="handcode"
        label="手工单号"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="handleEdit(scope)"
            >详情</el-button
          >
          <el-button
            type="success"
            v-if="scope.row.status == 1"
            size="mini"
            icon="el-icon-info"
            @click="batch(scope)"
            >批次详情</el-button
          >
          <!-- <el-button
            size="mini"
            type="primary"
            v-if="scope.row.status == 0"
            icon="el-icon-edit-outline"
            @click="confirms(scope)"
          >发货</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- 详情Dailog -->
    <sortGoodsDialog :dialog="dialog"></sortGoodsDialog>
    <!-- 批次详情Dailog -->
    <batchDialog :dialogs="dialogs"></batchDialog>
    <warehouseselect
      :warehouseselect="warehouseselect"
      @tag="warehouseTag"
    ></warehouseselect>
    <erpGoodsDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></erpGoodsDialog>
  </div>
</template>
<script>
import { parseTime, getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import SortGoodsDialog from "@/components/sortGoods/sortGoodsDialog";
import BatchDialog from "@/components/sortGoods/batchDialog";
import { erpCommodityList } from "@/api/commodity/commodityList";
import { getToken } from "@/utils/auth";
import Warehouseselect from "@/components/commodity/warehouseselect";
import ErpGoodsDialog from "@/components/grossMargin/erpGoodsDialog";

import {
  selectCargoList,
  getErpCargo,
  cargoOne
} from "@/api/sortGoods/sortGoodsList";
export default {
  components: {
    pagination: Pagination,
    sortGoodsDialog: SortGoodsDialog,
    batchDialog: BatchDialog,
    warehouseselect: Warehouseselect,
    erpGoodsDialog: ErpGoodsDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        searchCode: "",
        erpCode: [],
        status: "",
        start_time: "",
        end_time: "",
        hangcai: ""
      },
      outer: {
        options: [],
        value: "",
        list: [],
        loading: false,
        id: ""
      },
      hangcaiList: [
        {
          value: -1,
          label: "全部"
        },
        {
          value: 0,
          label: "非行采"
        },
        {
          value: 1,
          label: "行采"
        }
      ],
      options: [
        {
          value: 0,
          label: "待发货"
        },
        {
          value: 1,
          label: "已发货"
        },
        {
          value: 2,
          label: "已收货"
        }
      ],
      erpCenter: {
        date: new Date()
      },
      //dialog信息
      dialog: {
        centerDialogVisible: false,
        id: ""
      },
      //dialog批次信息
      dialogs: {
        centerDialogVisible: false,
        id: ""
      },
      warehouseselect: {
        centerDialogVisible: false
      },
      warehouseList: {
        name: "",
        code: ""
      },
      commoditydialog: {
        centerDialogVisible: false
      },
      commodityNameList: {
        name: "",
        code: ""
      },
      date: []
    };
  },

  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    //页面数据
    this.getData();
    //取初始商品列表
    this.getDatas();
  },
  methods: {
    //页面数据
    getData() {
      let CodeArr = [];
      CodeArr = this.search.erpCode;
      var code = CodeArr.join(",");

      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        warehouse_code: this.warehouseList.code,
        pickingcode: this.search.searchCode,
        status: this.search.status,
        productcode: this.commodityNameList.code,
        add_time: this.search.start_time,
        end_time: this.search.end_time,
        hangcai: this.search.hangcai == -1 ? "" : this.search.hangcai
      };
      selectCargoList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.cargo_list.data.forEach(res => {
              switch (res.status) {
                case 0:
                  res.statusMsg = "待发货";
                  break;
                case 1:
                  res.statusMsg = "已发货";
                  break;
                case 2:
                  res.statusMsg = "已收货";
                  break;
              }
            });
            this.tableData = res.data.cargo_list.data;
            this.pageData.page = res.data.cargo_list.current_page;
            this.pageData.pagesize = res.data.cargo_list.per_page;
            this.pageData.total = res.data.cargo_list.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    // 获取初始商品列表
    getDatas() {
      let data = {
        page: 1,
        limit: 50
      };
      erpCommodityList(data).then(res => {
        this.outer.options = res.data.data.map(item => {
          return { value: `${item.code}`, label: `${item.code}` };
        });
        console.log(this.outer);
      });
    },
    // 本地搜索
    handleSearch() {
      if (this.date) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //详情
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.id = scope.row.id;
    },
    batch(scope) {
      this.dialogs.centerDialogVisible = true;
      this.dialogs.id = scope.row.pickingid;
    },
    //清空
    empty() {
      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        searchCode: "",
        erpCode: [],
        status: "",
        start_time: this.normal_startime,
        end_time: this.normal_endtime,
        hangcai:''
      };
      this.erpCenter = {
        date: new Date()
      };
      this.warehouseList.name = "";
      this.warehouseList.code = "";
    },
    warehouseTag(data) {
      this.warehouseList = {
        name: "",
        code: ""
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.warehouseList.name = arrName.join(",");
      this.warehouseList.code = arrCode.join(",");
    },
    //超级仓库搜索
    warehouseBtn() {
      this.warehouseselect = {
        centerDialogVisible: true
      };
      this.commodityNameList = {
        name: "",
        code: ""
      };
      this.erpCenter = {
        date: new Date()
      };
    },
    //搜索商品数据
    remoteMethod(query) {
      if (query !== "") {
        this.getCommodityList(query);
      } else {
        this.outer.options = [];
      }
    },
    //获取商品列表
    getCommodityList(query) {
      let data = {
        search: query,
        page: 1,
        limit: 100
      };
      this.outer.loading = true;
      erpCommodityList(data)
        .then(res => {
          this.outer.list = res.data.data.map(item => {
            return { value: `${item.code}`, label: `${item.code}` };
          });
          this.outer.loading = false;
          this.outer.options = this.outer.list.filter(item => {
            return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
          });
          this.commodityList = res.data.data;
        })
        .catch(err => {
          console.log(err);
        });
    },
    //发货
    confirms(scope) {
      let data = {
        id: scope.row.id
      };
      this.$confirm("是否发货?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          cargoOne(data)
            .then(res => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg
                });
              }
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消发货"
          });
        });
    },
    //拉取erp
    pull(scope) {
      let data = {
        date: parseTime(this.erpCenter.date).split(" ")[0]
      };
      this.$confirm("是否拉取ERP?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          getErpCargo(data)
            .then(res => {
              if (res.code == 0) {
                this.search.date = this.erpCenter.date;
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg
                });
              }
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消拉取ERP"
          });
        });
    },
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          let hangcai =
            this.search.hangcai == -1 ? "" : this.search.hangcai;
          link.href = `${
            this.$axios.defaults.baseURL
          }/api/cargo/cargoExport?token=${getToken()}&pickingcode=${
            this.search.searchCode
          }&status=${this.search.status}&productcode=${
            this.commodityNameList.code
          }&hangcai=${hangcai}&add_time=${this.search.start_time}&warehouse_code=${
            this.warehouseList.code
          }&end_time=${this.search.end_time}&export=1`;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    derives() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          let hangcai =
            this.search.hangcai == -1 ? "" : this.search.hangcai;
          link.href = `${
            this.$axios.defaults.baseURL
          }/api/cargo/cargoExportXlsx?token=${getToken()}&pickingcode=${
            this.search.searchCode
          }&status=${this.search.status}&productcode=${
            this.commodityNameList.code
          }&hangcai=${hangcai}&add_time=${this.search.start_time}&warehouse_code=${
            this.warehouseList.code
          }&end_time=${this.search.end_time}&export=1`;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.btnList {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}

.pagination {
  text-align: right;
  padding: 10px;
}
</style>
